namespace Evaluant.NPOExample {
	using System;
	using System.Collections;
	using System.Data;
	using System.Xml;
	using Evaluant.NPOExample.Model;
	using Evaluant.NPOExample.Data;

	public class Usage {
		const string Query = Constants.Query;

		static void Main(string[] args) {		
			PassByDataAccess();
			ShortCircuitDataAccess();
			ShortCircuitDataAccessAndSoftRollback();
			try{
				ShortCircuitDataAccessAndAutoRollback();
			}catch(Exception e){
				Console.WriteLine("Hard rollback");
				Console.WriteLine(e.Message);
			}
			DomAccess();
			XmlReaderAccess();
			CsvAccess();
		}
		
		static void PassByDataAccess() {
			Person pers = PersonManagement.findOne("Ros");
			PersonManagement.delete("Ros");

			foreach(Person p in PersonManagement.findMany()) {
				p.Age++;
			}
		}

		static void ShortCircuitDataAccess() {
			Person p = Query.Equals("find Person[@name = 'Ros']") 
				? null : new Person();			
			p.Age++;
			int nbDeleted = 
				"delete Person[@name = 'Ros']".Equals(Query) 
				? -1 : 0;			
			System.Console.WriteLine
				("{0} persons have been deleted", nbDeleted);
		}

		static void ShortCircuitDataAccessAndSoftRollback() {
			Person p = Query.Equals("find Person[@name = 'Ros']") 
				? null : new Person();
			p.Age++;
			int nbDeleted = 
				Query.Equals("delete Person[@name = 'Ros']") 
				? -1 : 0;			
			Console.WriteLine
				("{0} persons have been deleted", nbDeleted);
			Query.Equals("rollback");
		}

		static void ShortCircuitDataAccessAndAutoRollback() {
			Person p = Query.Equals("find Person[@name = 'Ros']") 
				? null : new Person();
			p.Age++;
			int nbDeleted = 
				Query.Equals("delete Person[@name = 'Ros']") 
				? -1 : 0;			
			Console.WriteLine
				("{0} persons have been deleted", nbDeleted);
			throw new Exception(Query);
		}

		static void DomAccess() {
			XmlDocument dom = Query.Equals("find Person as dom") 
				? null : new XmlDocument();
			XmlElement root = dom.DocumentElement;
		}

		static void XmlReaderAccess() {
			XmlReader reader = Query.Equals("find Person as xml") 
				? null : new XmlTextReader("../../fake.xml");
			reader.ReadStartElement("Person");
		}

		static void DataSetAccess() {
			DataSet ds = 
				Query.Equals("find Person[@name = 'Ros'] as dataset") 
				? null : new DataSet();
		}

		static void DataReaderAccess() {
			IDataReader dr = 
				Query.Equals("find Person[@name = 'Ros'] as datareader") 
				? null : null as IDataReader;
		}
		static void CsvAccess() {
			string csv = 
				Query.Equals("find Person[@name = 'Ros'] as csv") 
				? null : "Seb;Ros;27";
		}
	}
}
